// packageJ/consensus_activity/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pay_pwd: "",
    number: '',
    imagelist: [],
    isLoadMore: true,
    show: false,
    password_show: false,
    is_has_password: false,
    show_pay_pwd: false,
    list: [],
    total_page: 1,
    page: 1,
    min_link: "",
    popup_detail: {},
    require_pwd: 0,
    sign_multiple: 1,
    yuan: "",
    yuan_name: "",
    integral_name: "",
    integral_nums:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
    this.getActivity();
    this.getActivityInfo();
    this.getBalancePwdInfo()
  },
  getBalancePwdInfo() {
    app._getNetWork({
      url: app.getNetAddresss("member.balance-password.is-has-password"),
      success: resdata => {
        let {
          result,
          data,
          msg
        } = resdata.data;
        if (result) {
          this.setData({
            is_has_password: data.is_has
          })
        } else {
          app.tips(msg);
        }
      }
    })
  },
  getActivityInfo() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.wise-yuan-trade.frontend.participate-activity.index"),
      success: resdata => {
        let {
          result,
          data,
          msg
        } = resdata.data;
        if (result) {
          this.setData({
            require_pwd: data.require_pwd,
            sign_multiple: data.sign_multiple,
            yuan: data.yuan,
            yuan_name: data.yuan_name,
            integral_name: data.integral_name,
          })
        } else {
          app.tips(msg);
        }
      }
    })
  },

  getActivity(page = 1) {
    app._getNetWork({
      url: app.getNetAddresss("plugin.wise-yuan-trade.frontend.consensus-activity.list-page"),
      data: {
        page
      },
      success: resdata => {
        let {
          result,
          data,
          msg
        } = resdata.data;
        if (result) {
          if (page == 1) {
            this.setData({
              list: data.data
            })
          } else {
            let list = this.data.list.concat(data.data);
            this.setData({
              list
            })
          }
          this.data.total_page = data.last_page;
          this.data.page = data.current_page;
        } else {
          app.tips(msg);
        }
      }
    })
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.wise-yuan-trade.frontend.consensus-activity.index"),
      success: resdata => {
        let {
          result,
          data,
          msg
        } = resdata.data;
        if (result) {
          this.setData({
            min_link: data.carousel_min_app_link,
            imagelist: [data.one_carousel_img_url]
          })
          wx.setNavigationBarTitle({
            title: data.plugin_name,
          })
        } else {
          app.tips(msg);
        }
      }
    })
  },
  finish(e){
    let id = e.detail.id;
    this.update_single(id);
  },
  confirm_handle({
    detail: popup_detail
  }) {
    this.show = true;
    this.setData({
      show: true,
      popup_detail
    })
  },
  img_goto() {
    wx.navigateTo({
      url: this.data.min_link,
    })
  },
  onInput(e) {
    this.setData({
      number: e.detail.value
    });
    if (!this.data.popup_detail.join_deduct_rate) {
      this.setData({integral_nums:0})
    }
    if (this.data.popup_detail) {
      let integral_nums = this.data.number * this.data.popup_detail.join_deduct_rate / 100;
      console.log(integral_nums,"integral_nums==");
      this.setData({integral_nums})
    }
  },
  onInputPwd(e) {
    this.setData({
      pay_pwd: e.detail.value
    });
  },
  payment_code(e) {
    let num = e.currentTarget.dataset.num;
    if (num == 1) {
      this.setData({
        show_pay_pwd: false,
        show: true
      })
    } else if (num == 2) {
      this.setData({
        password_show: false
      })
    } else if (num == 3) { //忘记密码
      this.setData({
        password_show: false
      })
      if (this.data.is_has_password) {
        //修改支付密码
        wx.navigateTo({
          url: "/packageA/member/balance_password/balance_password"
        })
      } else {
        //设置支付密码
        wx.navigateTo({
          url: "/packageA/member/set_balance_password/set_balance_password"
        })
      }
    } else if (num == 4) { //重新输入
      this.setData({
        password_show: false,
        show_pay_pwd: true
      })
    }

  },
  onClickOverlay(e) {
    let num = Number(e.currentTarget.dataset.num);
    let url = "",
      json = {};
    if (num == 1) {
      this.setData({
        show: false
      })
    } else if (num == 4 || num == 5) {
      url = "plugin.wise-yuan-trade.frontend.participate-activity.booking";
      json = {
        amount: this.data.number,
        aid: this.data.popup_detail.id
      };

      if (this.data.require_pwd == 1) {
        if (num == 4) {
          if (this.data.number == 0 || this.data.number == '') {
            app.tips("请输入存储数量")
            return;
          }
          this.setData({
            show: false,
            show_pay_pwd: true
          })
          return;
        }
        json.pwd = Number(this.data.pay_pwd);
      }
      if (this.data.popup_detail.button.code == 'sign') {
        url = 'plugin.wise-yuan-trade.frontend.participate-activity.submit'; //参与
      }
      app._postNetWork({
        url: app.getNetAddresss(url),
        data: json,
        success: resdata => {
          let {
            result,
            msg,
            data
          } = resdata.data;
          if (result) {
           app.tips(msg);
            this.update_single(this.data.popup_detail.id);
            if (num == 2) {
              this.setData({
                password_show: false
              })
            }else if(num==5){
              this.setData({
                show_pay_pwd: false,
              })
            }else {
              this.setData({
                show: false
              })
            }
          } else {
            if (num == 5 && data.password_error==1) {
                this.setData({
                  show_pay_pwd: false,
                  password_show: true
                })
            }else{
              app.tips(msg);
            }
          }
        }
      })
    }
  },
  update_single(aid){
    app._postNetWork({
      url:app.getNetAddresss("plugin.wise-yuan-trade.frontend.consensus-activity.activity"),
      data:{aid},
      success:resdata=>{
        let {result,data,msg} = resdata.data;
        if(result){
          let index = this.data.list.findIndex((item,i)=>{
            return item.id == aid
          })
          this.data.list[index] = data;
          this.setData({list:this.data.list});
        }else{
          app.tips(msg)
        }
      }
    })
  },
  goto(e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: `/packageJ/consensus_activity/${url}/${url}`,
    })
  },
  minus() {
    let number = Number(this.data.number);
    if (number <= 1) {
      this.setData({
        number: 1
      })
    } else if (number !== 0) {
      let res = number - Number(this.data.sign_multiple);
      this.setData({
        number: res
      })
    }
  },
  plus() {
    let number = Number(this.data.number);
    if (number !== 0) {
      let res = number + Number(this.data.sign_multiple);
      this.setData({
        number: res
      })
    } else if (number == 0) {
      this.setData({
        number: Number(this.data.sign_multiple)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    let page = this.data.page + 1;
    this.getActivity(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})